package com.example.automatic.abs_bank;

import android.accessibilityservice.AccessibilityService;
import android.os.Build;
import android.os.Bundle;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.RequiresApi;

import com.example.automatic.utils.LogUtils;
import com.example.automatic.utils.ToastUtils;

import java.util.List;

public class GDAccessibilityService extends AccessibilityService {

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        LogUtils.e("启动光大：");
        ToastUtils.showShort(this, "已启动");
    }



    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        LogUtils.e("光大：" + event.getWindowId());

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//                final AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
//
//                AccessibilityNodeInfo nodeInfo = findByID(accessibilityNodeInfo, "com.cebbank.mobile.cemb:id/et_transfer_input_first_transfer_payee_name");
//                if (nodeInfo != null) {
//                    Bundle arguments = new Bundle();
//                    arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
//                            "张三");
//                    nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
//                }
//            }
//        }).start();
//        try {
//            List<AccessibilityNodeInfo> nodeInfoList = accessibilityNodeInfo
//                    .findAccessibilityNodeInfosByViewId("com.cebbank.mobile.cemb:id/et_transfer_input_first_transfer_payee_name");
//            LogUtils.e("==============================" + nodeInfoList.size());
//            for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
//                LogUtils.e("====" + nodeInfo.getText());
//                nodeInfo.performAction(AccessibilityNodeInfo.FOCUS_INPUT);
////                Bundle arguments = new Bundle();
////                arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
////                        "林荔敏");
////                nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            LogUtils.e(e.getMessage());
//        }
    }

    @Override
    public void onInterrupt() {

    }

    //    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private AccessibilityNodeInfo findByID(AccessibilityNodeInfo rootInfo, String text) {
        if (rootInfo == null) {
            return null;
        }
        if (rootInfo.getChildCount() > 0) {
            for (int i = 0; i < rootInfo.getChildCount(); i++) {
                AccessibilityNodeInfo child = rootInfo.getChild(i);
                try {
                    List<AccessibilityNodeInfo> nodeInfoList = child.findAccessibilityNodeInfosByViewId(text);
                    if (nodeInfoList.size() > 0) {
                        return nodeInfoList.get(0);
                    }
                } catch (Exception e) {
                    return null;
                }
                findByID(child, text);//递归一直找一层层的全部遍历
            }
        }
        return null;
    }
}
